package com.ruoyi.worksite.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.worksite.domain.bo.SysMacRoleBo;
import com.ruoyi.worksite.domain.vo.SysMacRoleVo;
import com.ruoyi.worksite.domain.SysMacRole;
import com.ruoyi.worksite.mapper.SysMacRoleMapper;
import com.ruoyi.worksite.service.ISysMacRoleService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * MAC地址验证Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-23
 */
@RequiredArgsConstructor
@Service
public class SysMacRoleServiceImpl implements ISysMacRoleService {

    private final SysMacRoleMapper baseMapper;

    /**
     * 查询MAC地址验证
     */
    @Override
    public SysMacRoleVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询MAC地址验证列表
     */
    @Override
    public TableDataInfo<SysMacRoleVo> queryPageList(SysMacRoleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysMacRole> lqw = buildQueryWrapper(bo);
        Page<SysMacRoleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询MAC地址验证列表
     */
    @Override
    public List<SysMacRoleVo> queryList(SysMacRoleBo bo) {
        LambdaQueryWrapper<SysMacRole> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysMacRole> buildQueryWrapper(SysMacRoleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysMacRole> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMacAddr()), SysMacRole::getMacAddr, bo.getMacAddr());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysMacRole::getName, bo.getName());
        lqw.eq(bo.getEffectiveTime() != null, SysMacRole::getEffectiveTime, bo.getEffectiveTime());
        lqw.eq(bo.getExpirationTime() != null, SysMacRole::getExpirationTime, bo.getExpirationTime());
        return lqw;
    }

    /**
     * 新增MAC地址验证
     */
    @Override
    public Boolean insertByBo(SysMacRoleBo bo) {
        SysMacRole add = BeanUtil.toBean(bo, SysMacRole.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改MAC地址验证
     */
    @Override
    public Boolean updateByBo(SysMacRoleBo bo) {
        SysMacRole update = BeanUtil.toBean(bo, SysMacRole.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysMacRole entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除MAC地址验证
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean verify(String macAddr) {
        LambdaQueryWrapper<SysMacRole> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysMacRole::getMacAddr,macAddr);
        List<SysMacRole> list = baseMapper.selectList(wrapper);
        if (list == null || list.size() != 1){
            return false;
        }
        SysMacRole sysMacRole = list.get(0);
        DateTime now = DateUtil.date();
        Date startTime = sysMacRole.getEffectiveTime();;// 生效时间
        Date endTime = sysMacRole.getExpirationTime();// 失效时间
        return now.isAfterOrEquals(startTime) && now.isBeforeOrEquals(endTime);
    }
}
